#include "pwm_show.h"

PWM_Show::PWM_Show(QMainWindow *parent) : QDialog(parent)
{
    QFont fontNumber("宋体",13);
    zoom = 1;
    this->move(1000,0);
    this->setWindowTitle("电机PWM监视器@Avengers");
    this->setFixedSize(360,400); //360
    //从第100列分开，前边100列显示当前场前20场数据，后边200列显示当前场后40场数据
    //把这一个写出来，舵机的和速度的就能往上套了……
    this->repaint();
    re = new QPushButton(this);
    re->move(0,520);
    re->resize(50,30);
    re->setText("刷新");
    connect(re,SIGNAL(clicked(bool)),this,SLOT(repaint()));
    for (int i=0;i<5;i++)
    {
        Level[i] = new QLabel(this);
        Level[i]->setFont(fontNumber);
        Level[i]->setText(QString::number(i*20));
        Level[i]->resize(20,20);
        Level[i]->setAlignment(Qt::AlignCenter);
        Level[i]->move(0,280-60*i);
    }
    basePos = 0;


    //定义显示背景色和显示数据的标签
    QFont pwmNumberFont("宋体",20);
    leftPWMLabel = new QLabel(this);
    leftPWMLabel->setText("左PWM：");
    leftPWMLabel->resize(30,15);
    leftPWMLabel->move(0,350);
    LabelPalette = new QPalette();
    LabelPalette->setColor(QPalette::WindowText,Qt::red);
    leftPWMLabel->setPalette(*LabelPalette);

    leftPWMNumber = new QLabel(this);
    leftPWMNumber->move(30,345);
    leftPWMNumber->resize(60,20);
    leftPWMNumber->setFont(pwmNumberFont);
    rightPWMLabel = new QLabel(this);
    rightPWMLabel->setText("右PWM：");
    rightPWMLabel->resize(30,15);
    rightPWMLabel->move(0,380);
    LabelPalette->setColor(QPalette::WindowText,Qt::blue);
    rightPWMLabel->setPalette(*LabelPalette);

    rightPWMNumber = new QLabel(this);
    rightPWMNumber->move(30,375);
    rightPWMNumber->resize(60,20);
    rightPWMNumber->setFont(pwmNumberFont);
    //跟踪显示pwm值
    pwmTrace = new QLabel(this);
    pwmTrace->setText("Hello!");
    pwmTrace->resize(70,50);
    pwmTrace->setVisible(false);
    tracePalette = new QPalette();
    tracePalette->setColor(QPalette::Window,QColor(240,233,187));
    pwmTrace->setPalette(*tracePalette);
    pwmTrace->setAutoFillBackground(true);
    pwmTrace->setAlignment(Qt::AlignCenter);

    smaller = new QPushButton(this);
    smaller->move(0,360);
    smaller->setText("缩小");
    smaller->setVisible(false);
    smaller->resize(40,20);
    connect(smaller,SIGNAL(clicked(bool)),this,SLOT(Smaller()));

    larger = new QPushButton(this);
    larger->move(50,360);
    larger->setText("放大");
    larger->resize(40,20);
    larger->setVisible(false);
    connect(larger,SIGNAL(clicked(bool)),this,SLOT(Larger()));

    for (int i=0;i<6;i++)
        positionLabel[i] = new QLabel(this);

}
void PWM_Show::paintEvent(QPaintEvent *event)
{
    //果然现在看我半个月前写的代码都看不懂了  。。。。。。。。。。。。2016.12.30 元旦放假要回家辣开心~
    QPainter p(this);
    QPen pen(Qt::black,1);
    QPen pen2(Qt::black,1);
    float tPix;
    tPix = 2*zoom;
    if (tPix <1)
        tPix = 1;
    QPen pl(Qt::red,tPix);
    QPen pr(Qt::blue,tPix);
    int lastX = -1,lastY = -1;
    p.setPen(pen);
    for (int i=0;i<=5;i++)
        p.drawLine(QPoint(0,60*i),QPoint(360,60*i));
    p.setPen(pen2);
    p.drawLine(QPoint(120,0),QPoint(120,300));
    p.setPen(pl);
    for (int i=0;i<6;i++ )
        positionLabel[i]->setVisible(false);

    int lastPositionLabel = -1;
    for (int i=-40/zoom;i<=99/zoom;i++)
    {

        if (i+basePos>=0&&i+basePos<=Sum)
        {
            int currentPos = i+basePos;
            if (currentPos%30==0&&lastPositionLabel<=4)
            {
                positionLabel[++lastPositionLabel]->setText(QString::number(currentPos));
                positionLabel[lastPositionLabel]->setVisible(true);
                positionLabel[lastPositionLabel]->move((i-1)*3*zoom+120,300);
            }
        }
    }
    for (int i = -40/zoom;i<=99/zoom;i++)
    {
        if (i+basePos>=0&&i+basePos<=Sum)
        {

            if (lastX==-1&&lastY==-1)
                p.drawPoint(QPoint((i*3)*zoom+120,300-leftPWM[i+basePos]/10*3));
            else
            {
                p.drawLine(QPoint(lastX,lastY),QPoint((i*3)*zoom+120,300-leftPWM[i+basePos]/10*3));
            }
            //0-59 60-180 (x-60/3)+basePos = 当前帧
            lastX = i*3*zoom+120;
            lastY = 300-leftPWM[i+basePos]/10*3;
        }
    }
    lastX = -1;
    lastY = -1;
    p.setPen(pr);
    for (int i = -40/zoom;i<=99/zoom;i++)
    {
        if (i+basePos>=0&&i+basePos<=Sum)
        {
            if (lastX==-1&&lastY==-1)
                p.drawPoint(QPoint(i*3*zoom+120,300-rightPWM[i+basePos]/10*3));
            else
            {
                p.drawLine(QPoint(lastX,lastY),QPoint(i*3*zoom+120,300-rightPWM[i+basePos]/10*3));
            }
            lastX = i*3*zoom+120;
            lastY = 300-rightPWM[i+basePos]/10*3;
        }
    }
    //更新数值
    leftPWMNumber->setText(QString::number(leftPWM[basePos]/10.0));
    rightPWMNumber->setText(QString::number(rightPWM[basePos]/10.0));
}
void PWM_Show::mousePressEvent(QMouseEvent *event)
{
    int tx,ty;
    if (event->y()>300)
    {
        pwmTrace->setVisible(false);
        return;
    }
    if (event->x()-70<0)
        tx = 0;
    else
        tx = event->x()-70;
    if (event->y()-50<0)
        ty = 0;
    else
        ty = event->y()-50;
    pwmTrace->move(tx,ty);
    pwmTrace->setVisible(true);
    int currentMap = (event->x()-120)/(3*zoom)+basePos;
    if (currentMap<0)
    {
        pwmTrace->setText("无效位置！");
        return;
    }
    if (currentMap>Sum)
    {
        pwmTrace->setText("无效位置！");
        return;
    }

    pwmTrace->setText(QString("当前帧：")+QString::number(currentMap+1)
                      +QString("\n左PWM")+QString::number(leftPWM[currentMap]/10.0)
                      +QString("\n右PWM")+QString::number(rightPWM[currentMap]/10.0));


}
void PWM_Show::mouseMoveEvent(QMouseEvent *event)
{
    if (event->y()>300)
    {
        pwmTrace->setVisible(false);
        return;
    }
    int tx,ty;
    if (event->x()-70<0)
        tx = 0;
    else
        tx = event->x()-70;
    if (event->y()-50<0)
        ty = 0;
    else
        ty = event->y()-50;
    pwmTrace->move(tx,ty);
    pwmTrace->setVisible(true);
    int currentMap = (event->x()-120)/(3*zoom)+basePos;
    if (currentMap<0)
    {
        pwmTrace->setText("无效位置！");
        return;
    }
    if (currentMap>Sum)
    {
        pwmTrace->setText("无效位置！");
        return;
    }
    pwmTrace->setText(QString("当前帧：")+QString::number(currentMap+1)
                      +QString("\n左PWM")+QString::number(leftPWM[currentMap]/10.0)
                      +QString("\n右PWM")+QString::number(rightPWM[currentMap]/10.0));
}
void PWM_Show::wheelEvent(QWheelEvent *event)
{
    if (event->delta()>0)
        Larger();
    else
        Smaller();
}
